$(document).ready(function() {
	findAllRole();
	
	$.ajax({
		type : "post",
		url: "zlw/query_LoginRecord.action",
		success : showData
	});
	
	function getParams(p){
		$("#listEvent").empty();
		$("#loadingEvent").show();
		var page=p;
		var maxRow=$("#maxRow").text();
		var RName = $.trim($("#RName").val());
		var UId=$.trim($("#UId").val());
	    var UName=$.trim($("#UName").val());
		var TimeStart=$.trim($("#TimeStart").val());
		var TimeEnd=$.trim($("#TimeEnd").val());
		params = {
				"queryOption['page']":page,
				"queryOption['maxRow']":maxRow,
		 		"queryOption['RName']":RName,
				"queryOption['UId']":UId,
				"queryOption['UName']":UName,
				"queryOption['TimeStart']":TimeStart,
				"queryOption['TimeEnd']":TimeEnd
				};
		return params;
   }       
	
	//获取所有角色
	function findAllRole(){
		$.ajax({
  			type: "post",
 			url: "zlw/findAllRole_AllRole.action",
  			success:function(data){
  				$("<option value='0'>请选择</option>").appendTo(".uRole");
  				$.each(data.roles,function(i,n){
  					$("<option value="+n.RId+">"+n.RName+"</option>").appendTo(".uRole");
				});
  			}
  		});
	}
	
	//分页查询，异步提交
	function doAjax(d){
		$.ajax({
			type : "post",
			url: "zlw/query_LoginRecord.action",
			data : d,
			success : showData
		});
	}
	
	//删除
	function delAjax(d){
		$.ajax({
			type : "post",
			url: "zlw/delete_LoginRecord.action",
			data : d,
			success : function(data){
			    alert(data.message);
				$("#loadingEvent").hide();
				$("#deleteEvent").show();				
				$("#deleteEvent").hide();
				doAjax();
		}
		});
	}
    
	//根据条件查询 
	$("#query").click(function(){
		var timeStart=String($.trim($("#TimeStart").val()));
		var timeEnd=String($.trim($("#TimeEnd").val()));
        if(timeStart>timeEnd){
        	alert("你输入的开始时间大于结束时间！");
        	return;
        }else{
		$("#listEvent").fadeIn("fast");
		$("#loadingEvent").show();
		doAjax(getParams(1));
       }
	});
	
	//按时间段删除
	$("#delete").click(function(){
		var timeStart=String($.trim($("#TimeStart").val()));
		var timeEnd=String($.trim($("#TimeEnd").val()));
		if((timeStart>timeEnd)||timeStart==""||timeEnd==""){
        	alert("你输入的时间格式不正确！");
        	return;
        }
		if(!confirm("确定要删除吗？"))
		{
			return;
		}
		else{
		$("#eventTable").fadeIn("fast");
		delAjax(getParams(1));
       }
	});
	
	//导出
	$("#output").click(function(){
		var timeStart=String($.trim($("#TimeStart").val()));
		var timeEnd=String($.trim($("#TimeEnd").val()));
		if((timeStart>timeEnd)||timeStart==""||timeEnd==""){
	        alert("你输入的时间格式不正确！");
	        return;
	     }
		else {
			var url = "/ag/zlw/output_LoginRecord.action?timeStart="+timeStart+"&timeEnd="+timeEnd;
			window.location.href = url;
	        }
	});
			
	//分页查询，查第一页
	$("#firstPage").click(function(){
		doAjax(getParams(1));
	});
	//分页查询，查上一页
	$("#prePage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#page").text();
		page=(parseInt(page)-1);
		if(page<=0){
			alert("已经是第一页了！");
			return;
		}else{
		doAjax(getParams(page));
		}
	});
	//分页查询，查下一页
	$("#nextPage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#page").text();
		page=(parseInt(page)+1);
		if(page>pageCount){
			alert("已经是最后一页了！");
			return;
		}else{
		doAjax(getParams(page));
		}
	});
	//分页查询，查最后一页
	$("#lastPage").click(function(){
		var page=$("#pageCount").text();
		doAjax(getParams(page)); 
	});
	//分页查询，跳转到第n页
	$("#jumpPage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#jump").val();
		if(page>pageCount){
			alert("对不起，你输入的页数超过范围！");
			return;
		} else{
		doAjax(getParams(page)); 
		}
	});
	
	function showData(data){	
		$("#loadingEvent").show();
		$("#listEvent").empty();
		if(data.pageUtil.total==0){			
			$("#loadingEvent").hide();
			$("#noneEvent").show();
			$("#total").text(0);
			$("#page").text(0);
			$("#pageCount").text(0);
			$("#maxRow").text(0);			
		}else{
			$("#noneEvent").hide();
			$("#loadingEvent").hide();
		    $.each(data.pageUtil.list,function(i,n){
				fillDataToHtml(n.tbUser.UId,n.tbUser.UName,n.lrLoginTime,n.tbLogoutTime,n.tbUser.tbUserRoles);
				$("#total").text(data.pageUtil.total);
				$("#page").text(data.pageUtil.page);
				$("#pageCount").text(Math.ceil(data.pageUtil.total/10));
				$("#maxRow").text(data.pageUtil.maxRow);
		  	});
		    $("#listEvent").show();
		}
	}
	
	function fillDataToHtml(UId,UName,TimeStart,TimeEnd,tbUserRoles){
		var uRoles=new Array(tbUserRoles.length);
		$.each(tbUserRoles,function(i,n){
			uRoles[i] = "  "+n.tbRole.RName ;
		});
		var strHtml="<tr>";
        strHtml+="<td ><div>"+UId+"</div></td>";
        strHtml+="<td ><div>"+UName+"</div></td>";
        strHtml+="<td ><div>"+TimeStart.replace(/T/," ")+"</div></td>";
        if(TimeEnd==null){
        	strHtml+="<td ><div>无</div></td>";
        }else{
        strHtml+="<td ><div>"+TimeEnd.replace(/T/," ")+"</div></td>";
        }
        strHtml+="<td ><div>"+uRoles+"</div></td>";
        strHtml+="</tr>";
        $("#listEvent").append(strHtml);
	}	
});